import java.text.DateFormat


class VacationController {
  static allowedMethods = [delete:'POST', save:'POST', update:'POST']
  def index = { redirect(action:list,params:params) }

  def list = {
    params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
    [ vacationInstanceList: Vacation.list( params ), vacationInstanceTotal: Vacation.count() ]
  }

  private strDateDecorator = { "'" + g.format(date:it) + "'" }
  def show = {
    def vacationInstance = Vacation.get(params.id)
    if(!vacationInstance) {
      flash.message = "Vacation not found with id ${params.id}"
      redirect(action:list)
    }
    else { 
      return [vacationInstance:vacationInstance,
	      vacationsDates:"[" + vacationInstance.getDates(strDateDecorator).join(', ') + "]"
	     ] 
    }
  }

  def delete = {
    def vacationInstance = Vacation.get(params.id)
    if(vacationInstance) {
      try {
	vacationInstance.delete(flush:true)
	flash.message = "Vacation ${params.id} deleted"
	redirect(action:list)
      }
      catch(org.springframework.dao.DataIntegrityViolationException e) {
	flash.message = "Vacation ${params.id} could not be deleted"
	redirect(action:show,id:params.id)
      }
    }
    else {
      flash.message = "Vacation not found with id ${params.id}"
      redirect(action:list)
    }
  }

  def edit = {
    def vacationInstance = Vacation.get(params.id)
    if(!vacationInstance) {
      flash.message = "Vacation not found with id ${params.id}"
      redirect(action:list)
    }
    else { return [vacationInstance:vacationInstance] }
  }

  def update = {
    params.startDate = Date.parseOrNull(params.startDate)
    params.endDate = Date.parseOrNull(params.endDate)

    def vacationInstance = Vacation.get(params.id)
    if(vacationInstance) {
      if(params.version) {
	def version = params.version.toLong()
	if(vacationInstance.version > version) {                    
	  vacationInstance
	  .errors
	  .rejectValue("version", 
		       "vacation.optimistic.locking.failure", 
		       "Another user has updated this Vacation while you were editing.")
	  render(view:'edit',model:[vacationInstance:vacationInstance])
	  return
	}
      }
      vacationInstance.properties = params
      if(!vacationInstance.hasErrors() && vacationInstance.save()) {
	flash.message = "Vacation ${params.id} updated"
	redirect(action:show,id:vacationInstance.id)
      }
      else {
	render(view:'edit',model:[vacationInstance:vacationInstance])
      }
    }
    else {
      flash.message = "Vacation not found with id ${params.id}"
      redirect(action:list)
    }
  }

  def create = {
    def vacationInstance = new Vacation()
    vacationInstance.properties = params
    return ['vacationInstance':vacationInstance]
  }

  def save = {
    params.startDate = Date.parseOrNull(params.startDate)
    params.endDate = Date.parseOrNull(params.endDate)

    def vacationInstance = new Vacation(params)
    if(!vacationInstance.hasErrors() && vacationInstance.save()) {
      flash.message = "Vacation ${vacationInstance.id} created"
      redirect(action:show,id:vacationInstance.id)
    }
    else {
      render(view:'create',model:[vacationInstance:vacationInstance])
    }
  }
}
